﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace auxiliary_proj
{
    public partial class FormViewPO : Form
    {
        public FormViewPO()
        {
            InitializeComponent();
            dataGridOutStanding.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            qPO();            
        }

        public void qPO()
        {
            DataTable res = Query.select("SELECT * FROM purchase_order where status = '0' order by timestamp ASC");
            dataGridOutStanding.DataSource = res;
        }

        private void grid_DoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = ((DataGridView)sender).Rows[e.RowIndex];

            Console.Out.WriteLine(row.ToString());

            FormViewPODetails exists = Application.OpenForms.OfType<FormViewPODetails>().FirstOrDefault();
            if (exists == null)
            {
                FormViewPODetails poDetails = new FormViewPODetails(row);
                poDetails.StartPosition = FormStartPosition.CenterScreen;
                poDetails.Show();
            }
            else
            {
                exists.Close();
                exists.Dispose();
                FormViewPODetails poDetails = new FormViewPODetails(row);
                poDetails.StartPosition = FormStartPosition.CenterScreen;
                poDetails.Show();
            }
        }

        private void form_Closing(object sender, FormClosingEventArgs e)
        {
            //this form is closing, also close children (FormChangePass, )
            List<Form> close = new List<Form>();

            foreach (Form f in Application.OpenForms)
            {
                if (f is FormViewPODetails)
                    close.Add(f);
            }

            foreach (Form f in close)
            {
                f.Close();
                f.Dispose();
            }
        }
    }
}
